google.load('visualization', '1', {'packages':['motionchart']});
google.setOnLoadCallback(getRows);

function drawChart(rows) {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('date', 'Time');
    data.addColumn('number', 'Alcohol');
    data.addColumn('number', 'Drinks');
    data.addRows(rows);
    
    var chart = new google.visualization.MotionChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 600, height:300});
}

function getRows() {
    $.get('data', dataReceived);
}

function dataReceived(data) {
    var rows = data.split('\n');

    var datarows = [];
    
		var firstskipped = false;
    for (i in rows) {
				if (!firstskipped) {
					firstskipped = true;
					continue;
				}
        var row = rows[i];
        
        var columns = row.split(',');
        
        var datarow = [columns[1], getDate(columns[0]), Math.floor(columns[2] * 100.0), Math.floor(columns[3] * 100.0)];
        
        datarows.push(datarow);
    }

		alert(datarows);
    
    drawChart(datarows);
}

function getDate(str) {
    var date = new Date(str);

		var minutes = Math.round((new Date().getTime() - date.getTime()) / 1000000);
//		alert(minutes);
		var max = 2000;

		var fake = max - minutes;

		return new Date(fake, 0, 1);
}

